package phone.qyg.com.qyg.fragment.course.detail;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.base.tool.BaseFragment;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.text.MessageFormat;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.CourseDetail;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/11 16:53
 * 邮箱：lanbuhan@163.com
 * 功能：介绍
 */

public class IntroduceFragment extends BaseFragment {

    static String courseID;//课程id

    @ViewInject(R.id.title)
    TextView title;

    @ViewInject(R.id.regis_count)
    TextView regis_count;

    @ViewInject(R.id.release_time)
    TextView release_time;

    @ViewInject(R.id.video_count)
    TextView video_count;

    @ViewInject(R.id.introduce)
    TextView introduce;

    @ViewInject(R.id.course_introduce_content)
    TextView course_introduce_content;

    public static final IntroduceFragment newInstance(String courseID){
        IntroduceFragment fragment = new IntroduceFragment();
        Bundle bundle = new Bundle();
        bundle.putString(AppHttpKey.COURSE_ID, courseID);
        IntroduceFragment.courseID = courseID;
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater) {
        View view = View.inflate(QXApplication.getContext(), R.layout.course_introduce,null);
        x.view().inject(this,view);
        return view;
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        if (isRequestStr(courseID)) {
            courseID = getArguments().getString(AppHttpKey.COURSE_ID);
        }

        fMapKeys.put(AppHttpKey.TYPE,"0");
        fMapKeys.put(AppHttpKey.COURSE_ID,courseID);
        AppHttpRequest.httpRequestDefalut(ApplicationConfig.COURSE_DETAIL,
                fMapKeys,
                new AppHttpRequest.OnAppHttpListener() {
                    @Override
                    public void onAppHttpState(boolean isState, String result) {
                        if(isState && JSONTool.isStatus(result)){
                            CourseDetail courseDetail = JSONTool.jsonDefaluTranClazz(result,null, CourseDetail.class);
                            setData(courseDetail);
                        }
                    }
                });

    }


    private void setData(CourseDetail model){
        if(null == model) return;
        title.setText(setAttributeText(model.title));
        regis_count.setText(MessageFormat.format(QXApplication.getContext().getResources().getString(R.string.regis_count_key),
                            setAttributeText(model.regCount)));
        release_time.setText(MessageFormat.format(QXApplication.getContext().getResources().getString(R.string.release_key),
                            setAttributeText(model.startTime)));
        video_count.setText(MessageFormat.format(QXApplication.getContext().getResources().getString(R.string.video_count),
                            setAttributeText(String.valueOf(model.videoCount))));
        CommonUtil.textViewSetText(introduce,setAttributeText(model.lecturer));// //lecturer
        CommonUtil.textViewSetText(course_introduce_content,setAttributeText(model.detailContent));
    }

    @Override
    public void onNetChange(int netMobile) {}
}
